package com.ruoyi.system.domain;

import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import com.ruoyi.common.utils.ocr.OcrResultInfo;
import lombok.Data;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Data
@TableName("sys_ocr_template")
public class SysOcrTemplate {

    @TableId(type = IdType.AUTO)
    /** $column.columnComment */
    private Long templateId;

    /** 模板名称 */
    private String templateName;

    /** 模板关联的文件ID */
    private Long templateFileId;
    @TableField(exist = false)
    private SysFileLog sysFileLog;

    private Date createTime;

    @TableField(exist = false)
    private List<SysOcrTemplatePlace> places;

    public Map<String,String> handleOcrData(List<OcrResultInfo> ocrResultInfos) {
        Map<String,String> map = new HashMap<>();
        for (SysOcrTemplatePlace place : places) {
            List<SysOcrTemplatePoint> points = place.getPoints();
            double minXp = Math.min(new Double(points.get(0).getXp()),new Double(points.get(1).getXp()));
            double maxXp = Math.max(new Double(points.get(0).getXp()),new Double(points.get(1).getXp()));
            double minYp = Math.min(new Double(points.get(0).getYp()),new Double(points.get(1).getYp()));
            double maxYp = Math.max(new Double(points.get(0).getYp()),new Double(points.get(1).getYp()));
            StringBuilder value = new StringBuilder();
            for (OcrResultInfo info : ocrResultInfos) {
                double xPercent = info.getCenter().getXPercent();
                double yPercent = info.getCenter().getYPercent();
                if (xPercent >= minXp && xPercent <= maxXp
                && yPercent >= minYp && yPercent <= maxYp) {
                    value.append(info.getValue());
                }
            }
            map.put(place.getPlaceName(), value.toString());
        }
        return map;
    }
}
